package com.yj_app.qualitytest.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public abstract class CommonAdapter<T> extends BaseAdapter {
    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 实体类集合
     */
    private List<T> mDatas;
    private LayoutInflater mInflater;
    /**
     * 控件id
     */
    private int mlayoutId;

    public CommonAdapter(Context context, List<T> datas, int layoutId) {
        this.mContext = context;
        this.mDatas = datas;
        this.mlayoutId = layoutId;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mDatas.size();
    }

    @Override
    public T getItem(int arg0) {
        // TODO Auto-generated method stub
        return mDatas.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        ViewHolder holder = ViewHolder.get(mContext, arg1, arg2, mlayoutId,
                arg0);
        convert(holder, getItem(arg0));

        return holder.getConvertView();
    }

    public abstract void convert(ViewHolder holder, T t);

    public static class ViewHolder{
        /**
         * SparseArray类存放View集合
         */
        private SparseArray<View> mViews;
        /**
         *
         */
        private int mPosition;
        /**
         * 布局文件
         */
        private View mConvertView;

        public View getConvertView() {
            return mConvertView;
        }

        public ViewHolder(Context context, ViewGroup parent, int layoutId,
                          int position) {
            this.mViews = new SparseArray<View>();
            this.mPosition = position;
            this.mConvertView = LayoutInflater.from(context).inflate(layoutId,
                    parent, false);
            this.mConvertView.setTag(this);
        }
        /**
         * 拿到一个ViewHolder对象
         * @param context
         * @param convertView
         * @param parent
         * @param layoutId
         * @param position
         * @return
         */
        public static ViewHolder get(Context context, View convertView,
                                     ViewGroup parent, int layoutId, int position) {
            if (null == convertView) {
                return new ViewHolder(context, parent, layoutId, position);
            } else {
                ViewHolder holder = (ViewHolder) convertView.getTag();
                holder.mPosition = position;
                return holder;
            }
        }
        /**
         * 通过控件的id获取对应的控件，如果没有则加入views
         * @param viewId
         * @return
         */
        public <T extends View> T getView(int viewId) {
            View view = mViews.get(viewId);

            if (null == view) {
                view = mConvertView.findViewById(viewId);
                mViews.put(viewId, view);
            }

            return (T) view;
        }
        /**
         * 为TextView设置字符串
         * @param viewId
         * @param text
         * @return
         */
        public ViewHolder setText(int viewId, String text) {
            TextView tv = getView(viewId);
            tv.setText(text);
            return this;
        }
        /**
         * 为ImageView设置图片
         *
         * @param viewId
         * @param drawableId
         * @return
         */
        public ViewHolder setImageResource(int viewId, int drawableId)
        {
            ImageView view = getView(viewId);
            view.setImageResource(drawableId);
            return this;
        }


        public int getPosition()
        {
            return mPosition;
        }
    }
}
